package edu.njust.underwater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.njust.underwater.common.Result;
import edu.njust.underwater.exception.ResponseException;
import edu.njust.underwater.mapper.CommentMapper;
import edu.njust.underwater.pojo.Comment;
import edu.njust.underwater.pojo.dto.CommentDto;
import edu.njust.underwater.service.CommentService;
import edu.njust.underwater.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Goal
* @description 针对表【comment(评论表)】的数据库操作Service实现
* @createDate 2023-12-24 21:35:41
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{

    @Resource
    private UserService userService;

    @Override
    public Result getAll(Long articleId) {

        if (articleId == null) {
            throw new ResponseException("文章参数错误");
        }

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(Comment::getArticleId, articleId).orderBy(true, false, Comment::getLikes);
        List<Comment> comments = this.list(wrapper);

        List<CommentDto> dtos = comments.stream().map(comment -> {

            CommentDto dto = new CommentDto();
            String name = userService.getById(comment.getUserId()).getName();
            dto.setName(name);

            BeanUtils.copyProperties(comment, dto);

            return dto;
        }).toList();

        return Result.success(dtos);
    }

    @Override
    public Result addOne(Long articleId, Comment comment) {

        comment.setArticleId(articleId);
//        comment.setCreateTime(LocalDateTime.now());

        if (!this.save(comment)) {
            throw new ResponseException("评论失败");
        }

        return Result.success("评论成功");

    }

    @Override
    public Result sortBy(Long articleId, String sortType) {
        if (sortType == null || sortType.isEmpty()) {
            throw new ResponseException("参数错误");
        }

        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();

        if (sortType.equals("likes"))
            wrapper.eq(Comment::getArticleId, articleId).orderBy(true, false, Comment::getLikes);
        else
            wrapper.eq(Comment::getArticleId, articleId).orderBy(true, false, Comment::getCreateTime);

        List<Comment> comments = this.list(wrapper);
        List<CommentDto> dtos = comments.stream().map(comment -> {

            CommentDto dto = new CommentDto();
            String name = userService.getById(comment.getUserId()).getName();
            dto.setName(name);

            BeanUtils.copyProperties(comment, dto);

            return dto;
        }).toList();

        return Result.success(dtos);

    }

    @Override
    public Result addLike(Comment comment) {

        Comment temp = this.getById(comment.getId());

        temp.setLikes(temp.getLikes() + 1);

        this.updateById(temp);

        return Result.success("点赞成功");
    }
}
